package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class ExamServiceZyzImpl implements ExamService {
    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        try {
//            MasterDataService masterDataService = new MasterDataServiceGcsImpl()

            FileInputStream file = new FileInputStream(answerExcelFile);
            Workbook workbook = new XSSFWorkbook(file);

            ArrayList<Answer> answerList = new ArrayList<>();

            Sheet answerSheet = workbook.getSheetAt(1);
            Iterator<Row> rowIterator = answerSheet.rowIterator();
            Row rowStudent = rowIterator.next();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                cellIterator.next();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    Student student = new Student(null, null,
                            rowStudent.getCell(cell.getColumnIndex()).getRichStringCellValue().getString());

                    char choice = cell.getRichStringCellValue().getString().charAt(0);
                    Answer answer = new Answer(null, null, null, choice);
                    answerList.add(answer);
                }
            }

        }
        catch (FileNotFoundException e) {
            log.error("importCategories FileInputStream FileNotFoundException\n");
        }
        catch (IOException e) {
            log.error("importCategories XSSFWorkbook IOException\n");
        }

        return null;
    }
}
